package com.example.activity.data;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.activity.data.databinding.ActivityFirstBinding;

import java.util.ArrayList;

public class FirstActivity extends AppCompatActivity
        implements View.OnClickListener {
    private ActivityFirstBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFirstBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnData.setOnClickListener(this);
        binding.btnList.setOnClickListener(this);
        binding.btnObject.setOnClickListener(this);
        binding.btnBundle.setOnClickListener(this);
        binding.btnReturn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        switch (v.getId()) {
            case R.id.btn_data:
                intent.putExtra("data", "Activity传递字符串");
                startActivity(intent);
                break;
            case R.id.btn_list:
                ArrayList<Integer> datas = new ArrayList<>();
                datas.add(85);
                datas.add(90);
                datas.add(78);
                intent.putIntegerArrayListExtra("list", datas);
                startActivity(intent);
                break;
            case R.id.btn_object:
                User user = new User("王晓", 20);
                intent.putExtra("object", user);
                startActivity(intent);
                break;
            case R.id.btn_bundle:
                Bundle bundle = new Bundle();
                bundle.putString("username", "王晓");
                bundle.putInt("age", 20);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.btn_return:
                intent.putExtra("data", "传递字符串");
//                startActivityForResult(intent, 1);
//                launcher.launch("传递字符串");
//                myLauncher.launch(intent);
//                contractLauncher.launch(null);
                picLauncher.launch(null);
//                permissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
        }
    }
    private ActivityResultLauncher<String> launcher = registerForActivityResult(
            new ResultContract(),
            new ActivityResultCallback<String>() {
                @Override
                public void onActivityResult(String result) {
                    binding.tvData.setText("返回的数据为：" + result);
                    Log.i("FirstActivity", "返回的数据为：" + result);
                }
            });

    class ResultContract extends ActivityResultContract<String, String> {
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            intent.putExtra("data", input);
            return intent;
        }
        @Override
        public String parseResult(int resultCode, @Nullable Intent intent) {
            if (resultCode == Activity.RESULT_OK && intent != null) {
                return intent.getStringExtra("data");
            }
            return null;
        }
    }

    private ActivityResultLauncher<Intent> myLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        Intent data = result.getData();
                        if (data != null) {
                            binding.tvData.setText("回传数据：" + data.getStringExtra("data"));
                        }
                    }
                }
            });

    private ActivityResultLauncher<Void> contractLauncher = registerForActivityResult(
            new ActivityResultContracts.PickContact(),
            new ActivityResultCallback<Uri>() {
                @Override
                public void onActivityResult(Uri uri) {
                    if (uri != null) {
                        final Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                        if (cursor.moveToFirst()) {
                            final String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                            binding.tvData.setText("通讯录姓名：" + name);
                        }

                    }
                }
            });

    private ActivityResultLauncher<Void> picLauncher = registerForActivityResult(
            new ActivityResultContracts.TakePicturePreview(),
            new ActivityResultCallback<Bitmap>() {
                @Override
                public void onActivityResult(Bitmap bitmap) {
                    binding.imageView.setImageBitmap(bitmap);
                    binding.imageView.setVisibility(View.VISIBLE);
                }
            });

    ActivityResultLauncher<String> permissionLauncher = registerForActivityResult(
            new ActivityResultContracts.RequestPermission(),
            new ActivityResultCallback<Boolean>() {
                @Override
                public void onActivityResult(Boolean isGrant) {
                    if (isGrant) {
                        // 用户授予权限
                    } else {
                        Toast.makeText(FirstActivity.this, "授予权限不允许", Toast.LENGTH_SHORT).show();
                    }
                }
            });

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 根据请求码 requestCode进行判断
        if (requestCode == 1) {
            if (resultCode == RESULT_OK && data != null) {
                // 通过 Intent对象获取返回的数据
                String returnData = data.getStringExtra("data");
                // 判断数据是否为 null
                if (returnData != null) {
                    binding.tvData.setText("返回的数据为：" + returnData);
                }
            }
        }
    }


}